package com.example.administrator.ygddriver.set.safety.setpassword;

import android.content.Intent;
import android.support.v7.widget.CardView;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;

import com.example.administrator.ygddriver.R;
import com.example.administrator.ygddriver.base.BaseActivity;
import com.example.administrator.ygddriver.base.BaseHint;
import com.example.administrator.ygddriver.into.register.forget.reset.ResetRequestBean;
import com.example.administrator.ygddriver.set.SetActivity;
import com.example.administrator.ygddriver.tool.OkGoRequests;
import com.example.administrator.ygddriver.tool.SimpleToolbar;
import com.example.administrator.ygddriver.tool.UrlValuse;

import java.util.HashMap;

public class SetPasswordActivity extends BaseActivity implements View.OnClickListener {
    private SimpleToolbar simpleToolbar;
    private CardView mNextCv;
    private EditText mPasswordEt;
    private String phone;

    @Override
    public int initLayout() {
        return R.layout.activity_set_password;
    }

    @Override
    public void initView() {
        simpleToolbar = findViewById(R.id.activity_set_password_stb);
        mNextCv = findViewById(R.id.activity_set_password_cv_next);
        mPasswordEt = findViewById(R.id.activity_set_password_et);
        mNextCv.setOnClickListener(this);
    }

    @Override
    public void initDate() {
        phone = getIntent().getStringExtra("phone");
        setSimpleToolbar();

    }

    private void setSimpleToolbar() {
        simpleToolbar.setMainTitle("密码设置");
        simpleToolbar.setMainTitleColor("#000000");
        simpleToolbar.setBackIv(R.mipmap.u126);
        simpleToolbar.setToolColor("#ffffff");
        simpleToolbar.setBackIvClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.activity_set_password_cv_next:
                if (!TextUtils.isEmpty(mPasswordEt.getText().toString()) && mPasswordEt.getText().toString().length() > 5) {
                    initUpdatePasswordHtml(phone, mPasswordEt.getText().toString());
                } else {
                    BaseHint baseHint = new BaseHint.addHint().setParameter(SetPasswordActivity.this, "提示", "密码长度不正确");
                    baseHint.show();
                }

                break;
        }
    }

    //忘记密码 更新密码
    private void initUpdatePasswordHtml(String phone, String password) {
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("c_account", phone);
        hashMap.put("c_password", password);
        hashMap.put("c_type", "1");
        OkGoRequests.getInstance().putRequestAsync(UrlValuse.POST_API_BASE + UrlValuse.UPDATE_PASSWORD, hashMap, ResetRequestBean.class, new OkGoRequests.OnTrue<ResetRequestBean>() {
            @Override
            public void hasData(ResetRequestBean date) {
                if (date.isSuccess()) {
                    Intent intent = new Intent(SetPasswordActivity.this, SetActivity.class);
                    startActivity(intent);
                    finish();
                } else {
                    BaseHint baseHint = new BaseHint.addHint().setParameter(SetPasswordActivity.this, "提示", date.getLocalizedMessage());
                    baseHint.show();
                }

            }
        }, new OkGoRequests.OnError<ResetRequestBean>() {
            @Override
            public void noData() {

            }
        });

    }
}
